<?php
class klausuren_controller extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		if($this->session->userdata('logged_in'))
   		{
			$this->load->model('klausuren_model','',TRUE);
			$this->load->model('fragen_model');
			$this->load->model('faecher_model');
			$this->load->model('noten_model');
			$this->load->model('kurse_model');
			$this->load->model('klausurenschueler_model');
		}
   		else
   		{
     		//If no session, redirect to login page
     		redirect('welcome', 'refresh');
   		}
	}

	public function index()
	{
		$user_data = $this->session->userdata('logged_in');
   		if($this->session->userdata('logged_in') && ($user_data['role'] == 'lehrer'))
   		{
			$data['klausuren'] = $this->klausuren_model->get_klausuren();
	
			$data['_scripts'] = '';
		 	$data['_styles'] = '';
			$data['head'] = '';
			$data['schulname'] = 'Berufskolleg Uerdingen';
			$data['navi'] = $this->load->view('login/lehrernavi_view', $data,TRUE);
			$data['login'] = '';
			$data['content'] = $this->load->view('klausuren/klausurenindex_view', $data,TRUE);
			$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
			
			$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
		}
   		else
   		{
     		//If no session, redirect to login page
     		show_error('403 FORBIDDEN',$status_code= 403);
   		}
	}
	
	public function create_view()
	{
		$user_data = $this->session->userdata('logged_in');
   		if($this->session->userdata('logged_in') && ($user_data['role'] == 'lehrer'))
   		{
			$data['faecher'] = $this->faecher_model->get_faecher();
			$data['noten'] = $this->noten_model->get_noten();
			$data['kurse'] = $this->kurse_model->get_kurse();
				
			$data['_scripts'] = '';
		 	$data['_styles'] = '';
			$data['head'] = '';
			$data['schulname'] = 'Berufskolleg Uerdingen';
			$data['navi'] = $this->load->view('login/lehrernavi_view', $data,TRUE);
			$data['login'] = '';
			$data['content'] = $this->load->view('klausuren/klausurencreate_view', $data,TRUE);
			$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
			
			$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
		}
   		else
   		{
     		//If no session, redirect to login page
     		show_error('403 FORBIDDEN',$status_code= 403);
   		}
	}
	
	public function create()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');

		$this->form_validation->set_rules('Beschreibung', 'Beschreibung', 'required');														   
		$this->form_validation->set_rules('Password', 'Password', 'trim|required|xss_clean');	

		if ($this->form_validation->run() === FALSE)
		{
			$this->create_view();
		}
		else
		{
			$this->klausuren_model->set_klausuren();	
			$this->index();
		}
	}
	
	public function validate_view()
	{
		$user_data = $this->session->userdata('logged_in');
   		if($this->session->userdata('logged_in') && ($user_data['role'] == 'lehrer'))
   		{
			$klausurenid = $this->input->post('klausurenid');
			$data['klausuren'] = $this->klausuren_model->get_klausuren_id($klausurenid);
			$data['faecher'] = $this->faecher_model->get_faecher();
			$data['noten'] = $this->noten_model->get_noten();
			$data['kurse'] = $this->kurse_model->get_kurse();
			
			$data['_scripts'] = '';
		 	$data['_styles'] = '';
			$data['head'] = '';
			$data['schulname'] = 'Berufskolleg Uerdingen';
			$data['navi'] = $this->load->view('login/lehrernavi_view', $data,TRUE);
			$data['login'] = '';
			$data['content'] = $this->load->view('klausuren/klausurenvali_view', $data,TRUE);
			$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
			
			$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
		}
   		else
   		{
     		//If no session, redirect to login page
     		show_error('403 FORBIDDEN',$status_code= 403);
   		}
	}
	
	public function validate()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');

		$this->form_validation->set_rules('Beschreibung', 'Beschreibung', 'required');														   
		$this->form_validation->set_rules('Fach', 'Fach', 'required');

		if ($this->form_validation->run() === FALSE)
		{	
			#redirect('user_controller','refresh');
			$this->validate_view();
		}
		else
		{
			$klausurenid= $this->input->post('klausurenid');
			$this->klausuren_model->validate_klausuren($klausurenid);

			$this->index();
		}	 
	}

	public function klausurfragen_view()
	{
		$user_data = $this->session->userdata('logged_in');
   		if($this->session->userdata('logged_in') && ($user_data['role'] == 'lehrer'))
   		{
			$data['klausurenid'] = $this->input->post('klausurenid');
			$data['anzahlfragen'] = $this->input->post('anzahlfragen');
			#$data['klausuren'] = $this->klausuren_model->get_klausuren_id($klausurenid);
			$data['fragen'] = $this->fragen_model->get_fragen();
			
			$data['_scripts'] = '';
		 	$data['_styles'] = '';
			$data['head'] = '';
			$data['schulname'] = 'Berufskolleg Uerdingen';
			$data['navi'] = $this->load->view('login/lehrernavi_view', $data,TRUE);
			$data['login'] = '';
			$data['content'] = $this->load->view('klausuren/klausurenfragen_view2', $data,TRUE);
			$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
			
			$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
		}
   		else
   		{
     		//If no session, redirect to login page
     		show_error('403 FORBIDDEN',$status_code= 403);
   		}
	}
	
	public function set_klausurfragen()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');
		
		#$klausurfragen['klausurenid'] = $this->input->post('klausurenid');
		$klausurfragen = $_POST['fragenid'];

		$this->klausurenfragen_model->set_klausurenfragen($data);	
		$this->index();

		print_r($_POST);
	}

	public function klausurenlistview($page = 0,$fach = 'Alle',$kurs ='Alle')
	{
		$user_data = $this->session->userdata('logged_in');
   		if($this->session->userdata('logged_in') && ($user_data['role'] == 'lehrer'))
   		{	
			$limit = 10;

			$data['selectedfach'] = $fach;
			$data['selectedkurs'] = $kurs;
			$data['faecher'] = $this->faecher_model->get_faecher();
			$data['kurse'] = $this->kurse_model->get_kurse();
			$data['klausuren'] = $this->klausuren_model->get_fachkursklausur($fach,$kurs,$limit, $page * $limit);
			$data['pages'] = ceil($this->klausuren_model->get_fachkurscount($fach,$kurs) / $limit);
			
			$data['_scripts'] = '';
		 	$data['_styles'] = '';
			$data['head'] = '';
			$data['schulname'] = 'Berufskolleg Uerdingen';
			$data['navi'] = $this->load->view('login/lehrernavi_view', $data,TRUE);
			$data['login'] = '';
			$data['content'] = $this->load->view('klausuren/klausurenlist_view',$data,TRUE);
			$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
			
			$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
		}
   		else
   		{
     		//If no session, redirect to login page
     		show_error('403 FORBIDDEN',$status_code= 403);
   		}
	}
	
	public function checkclasstestrelease()
	{
		$user_data = $this->session->userdata('logged_in');
   		if($this->session->userdata('logged_in') && ($user_data['role'] == 'schueler'))
   		{
			IF ($this->input->post('klausurenid') != '') {
			    $sess_array = array(
			         'ID' => $this->input->post('klausurenid'),
			         'anzahlfragen' => $this->input->post('anzahlfragen')
			       );
			    $this->session->set_userdata('klausuren_data', $sess_array);
				$data['klausuren'] = $this->klausuren_model->get_klausuren_id($this->input->post('klausurenid'));
			}
			else {
				$klausuren_data = $this->session->userdata('klausuren_data');
				$data['klausuren'] = $this->klausuren_model->get_klausuren_id($klausuren_data['ID']);
			}
			$abgegeben = $this->klausurenschueler_model->get_klausurabgegeben($data['klausuren']['ID'],$user_data['id']);
			if($abgegeben == 0)
			{
				$data['_scripts'] = '';
			  	$data['_styles'] = '';
			 	$data['head'] = '';
				$data['schulname'] = 'Berufskolleg Uerdingen';
				$data['navi'] = $this->load->view('login/schuelernavi_view', $data,TRUE);
				$data['login'] = '';
				$data['content'] = $this->load->view('klausuren/klausurenlogin_view', $data,TRUE);
				$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
				
				$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
			}
			else 
			{
				redirect('schueler_controller/schuelerklausuren_view', 'refresh');
			}
		}
   		else
   		{
     		//If no session, redirect to login page
     		show_error('403 FORBIDDEN',$status_code= 403);
   		}
	}
	
	function klausurlogin()
   {
   		//This method will have the credentials validation
	   $this->load->library('form_validation');
	
	   //$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
	   $this->form_validation->set_rules('password', 'password', 'trim|required|xss_clean|callback_klausuren_check_database');
	
	   if($this->form_validation->run() == FALSE)
	   {
	   		$this->checkclasstestrelease();
	   }
	   else
	   {
	     //Go to private area
	     redirect('klausurenfragen_controller/classtest_view', 'refresh');
	   }
   }
   
   function klausuren_check_database($password)
   {
	   //Field validation succeeded.&nbsp; Validate against database
	   $klausur = $this->input->post('klausurid');
	
	   //query the database
	   $result = $this->klausuren_model->klausurlogin($klausur, $password);
	
	   if($result)
	   {
	     $sess_array = array();
	     foreach($result as $row)
	     {
	       $sess_array = array(
	         'ID' => $row->ID,
	         'anzahlfragen' => $row->AnzahlFragen
	       );
	       $this->session->set_userdata('klausuren_data', $sess_array);
	     }
		 
	     return TRUE;
	   }
	   else
	   {
	     $this->form_validation->set_message('klausuren_check_database', 'Invalid password or ClassTest not Released');
	     return false;
	   }
   }
}